/**
 * Company : SIANGKLOM.
 * Created By : Chatree Siangklom
 * Created On : Oct 7, 2013  5:12:30 PM 
 * e-Mail : dr_chatree@hotmail.com
 * Mobile Phone : (+66) 087-921-5115
 * Package Name : test.sap
 * File Name : sdCustomerAction.java
 * TODO
 */
package test.sap;

import java.util.Calendar;

import com.sap.conn.jco.JCoFunction;
import com.sap.conn.jco.JCoStructure;
import com.sap.conn.jco.JCoTable;

import sap.jco.client.Connection;


/**
 * @author Chatree Siangklom of Administrator
 * Date =Oct 7, 2013 : 5:12:30 PM 
 * CreateBy=Chatree Siangklom
 * 
 * sdCustomerAction
 * TODO
 */

public class sdCustomerAction  {
	
	private String sap_bapi;
	private String sap_table;
	private String sap_structure;
	private String sap_table_return;
	private String sap_result_return;
	
	//Sap 
	private Connection jco_conn;
	
	
	private void Start() throws Exception {

		this.jco_conn = new Connection();
		JCoTable it_output;
        Calendar calendar = Calendar.getInstance();

		JCoFunction jcoFunction = this.jco_conn.getJCoFunction("ZSASD0001");

        if (jcoFunction == null)
            throw new RuntimeException("ZSASD0001 not found in SAP.");

        JCoTable jcoTable = jcoFunction.getTableParameterList().getTable("IT_CUSTPROFILE");
        
        long strSTCD3 = 3000000000001L;
        calendar.setTimeInMillis(System.currentTimeMillis());
        System.out.println("Start at " + calendar.get(Calendar.HOUR) + ":" + calendar.get(Calendar.MINUTE) + ":" + calendar.get(Calendar.SECOND));
        
        for(int i=0;i<1;++i)
        {
        	//strSTCD3 += 1;
        	
	        jcoTable.appendRow();
	        //item 1
	        jcoTable.setRow(0);
	        jcoTable.setValue("PMS_STAGING_ID", "TEST0000000002");
	        jcoTable.setValue("KTOKD", "1100");		// Account Group
	        jcoTable.setValue("KDGRP", "11");		// Customer Group
	        jcoTable.setValue("VTWEG", "10");		// Distribution Channel
	        jcoTable.setValue("SPART", "ZZ");		// Division
	        jcoTable.setValue("NAME1", "chatree");	// Name1
	        jcoTable.setValue("NAME2", "siangklom");	// Name2
	        jcoTable.setValue("SORTL", "ccsc1");	// Search Term
	        jcoTable.setValue("STRAS", "ccsc1Street");	// Street
	        jcoTable.setValue("LAND1", "TH");		// Country
	        jcoTable.setValue("STCD3", strSTCD3);		// Tax Code 13 Digit
	        jcoTable.setValue("VERSG", "1");		// Cus.status grp
	        jcoTable.setValue("ZTERM", "N30");		// Payment Term
	        jcoTable.setValue("TAXKD", "1");		// Tax classification
	        jcoTable.setValue("AKONT", "11020101");	// Recon.Account
	        jcoTable.setValue("MAHNA", "Z005");		// Dun.Procedure
			jcoTable.setValue("CREATEBY", "CCSC");
			jcoTable.setValue("CREATEON", "2013-09-13//11:11:00");
			jcoTable.setValue("UPDATEBY", "CCSC");
			jcoTable.setValue("UPDATEON", "2013-09-13//11:11:00");
	
	        //item 2
	        //jcoTable.setRow(1);
	        //jcoTable.setValue("XXX", "XXXXX");
	        
	        try
	        {
				jcoFunction.execute(jco_conn.getJCoConnection());
	        }
	        catch (Exception e)
	        {
	            System.out.println(e.toString());
	            return;
	        }
	        jcoTable.deleteAllRows();
	
	        //System.out.println("ZSASD0001 finished:");
	        it_output = jcoFunction.getTableParameterList().getTable("IT_OUTPUT");

	        calendar.setTimeInMillis(System.currentTimeMillis());
	        System.out.print(calendar.get(Calendar.HOUR) + ":" + calendar.get(Calendar.MINUTE) + ":" + calendar.get(Calendar.SECOND) + " ");
	        System.out.print("SAP_STAGING_ID: " + it_output.getString("SAP_STAGING_ID") + "\r\n");
	        System.out.print("KUNNR: " + it_output.getString("KUNNR") + "\r\n");
	        System.out.print("MSG_CODE: " + it_output.getString("MSG_CODE") + "\r\n");
	        System.out.print("MSG_DESC: " + it_output.getString("MSG_DESC") + "\r\n");
        }

        calendar.setTimeInMillis(System.currentTimeMillis());
        System.out.println("End at " + calendar.get(Calendar.HOUR) + ":" + calendar.get(Calendar.MINUTE) + ":" + calendar.get(Calendar.SECOND));
        System.out.println("Last STCD3: " + strSTCD3);
                

        /*System.out.print(it_output.getString("KUNNR") + " ");
        System.out.print(it_output.getString("ANRED") + " ");
        System.out.print(it_output.getString("NAME1") + " ");
        System.out.print(it_output.getString("NAME2") + " ");
        System.out.print(it_output.getString("NAME3") + " ");
        System.out.print(it_output.getString("NAME4") + "\n\r");
        System.out.println("MSGTYPE: " + it_output.getString("MSGTYPE"));
        System.out.println("MSGTXT: " + it_output.getString("MSGTXT"));*/

		
		
/*		

		SAPConnectionManager objj = null;
		
		try{
			
			//new Connection().CreatedJCOConnectionProperty();
			
			
			this.sap_bapi = "ZSASD0001";
			this.sap_structure = "IT_CUSTPROFILE";
			this.sap_table_return = "RETURN";
			this.sap_result_return = "IT_OUTPUT";
			
			objj = new SAPConnectionManager();
			objj.init();
			objj.setFunctionTemplate(this.sap_bapi);

			List<sdCustomerModel> lst = new ArrayList<sdCustomerModel>();
			sdCustomerModel model = new sdCustomerModel();
			
			//model.setKUNNR("");
			model.setKTOKD("1100");
			model.setKDGRP("11");
			model.setVTWEG("10");
			model.setSPART("ZZ");
			model.setNAME1("Test1");
			model.setSORTL("Test1");
			model.setSTRAS("TestStreet");
			model.setLAND1("TH");
			model.setVERSG("1");
			model.setZTERM("N30");
			model.setTAXKD("1");
			model.setSTCD3("1234567890150");
			model.setAKONT("11020101");
			model.setMAHNA("Z005");
			
			lst.add(model);
						

			this.jco_conn = new Connection();
			//jco_conn.JcoTestCompany();
			
			//Get SAP BAPI function
			JCoFunction jcoFunction = this.jco_conn.getJCoFunction(this.sap_bapi);
			
			//Get SAP Staging table
			JCoStructure jcoStructure = this.jco_conn.getStructure().getStructure(this.sap_structure);
			new SdOutboudService().SynchroToSap(jcoFunction, jcoStructure, lst);
			
			//Get SAP Staging table retrun message
			JCoTable jcoTable_return =  this.jco_conn.getTables(this.sap_result_return);
			CallbackMessageFromSap(jcoTable_return);
			do
			{
				jcoTable_return.nextRow();
				System.out.println(jcoTable_return.getString("KUNNR"));
			}while(jcoTable_return.isLastRow());
			
		}catch(Exception e){
			e.printStackTrace();
		}finally{ objj.release(); }*/
	}
	
	public static void main(String[] args) {
		
	try{
		
		new sdCustomerAction().Start();
		
		
		
		
	}catch(Exception e){
		e.printStackTrace();
	}

	}

}
